package com.general.message.notice.aspect;

import com.general.common.utils.SecurityUtils;
import com.general.message.notice.annotation.NoticePush;
import com.general.message.notice.entity.NoticeInfo;
import com.general.message.notice.enums.NoticeType;
import com.general.message.notice.service.INoticeInfoService;
import com.general.message.push.websocket.WsSupport;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Aspect
@Component
public class NoticePushAspect {

    @Pointcut("@annotation(com.general.message.notice.annotation.NoticePush)")
    public void pointcut() {}

    @Autowired
    private INoticeInfoService noticeInfoService;

    /**
     * 运行完成后提醒
     * 通用型
     */
    @SuppressWarnings("unchecked")
    @AfterReturning(pointcut = "@annotation(annotation)", returning = "jsonResult")
    public void noticeAfterRunCompletePush(JoinPoint joinPoint, NoticePush annotation, Object jsonResult) {

        NoticeType noticeType = annotation.noticeType();

        if (noticeType == null) {
            log.error("通知属性为空，请检查完善通知内容！");
            return;
        }

        NoticeInfo noticeInfo = new NoticeInfo();
        String menuId = "";//菜单id
        // 从注解中获取参数
        if (noticeType == NoticeType.ISNULL) {
            // 没有预先设置参数, 从注解属性中获取
            noticeInfo.setTitle(annotation.title());
            noticeInfo.setContent(annotation.content());
            noticeInfo.setType(annotation.type());
            noticeInfo.setMenuKey(annotation.menuKey());
            menuId = annotation.menuId();
        } else {
            noticeInfo.setTitle(noticeType.getTitle());
            noticeInfo.setContent(noticeType.getContent());
            noticeInfo.setType(noticeType.getType());
            noticeInfo.setMenuKey(noticeType.getMenuKey());
            menuId = noticeType.getMenuId();
        }
        noticeInfo.setFromUser(SecurityUtils.getUserId().toString());
        //获取消息接收人id
//        noticeInfo.setReceiveUser("2");

//        System.out.println("fromUser-->" + ((Map)jsonResult).get("fromUser").toString());
//        System.out.println("fromUser-->" + sysUser.getUserId());
//        System.out.println("noticeInfo-->" + noticeInfo);
//        System.out.println("jsonResult-->" + jsonResult);

        //获取待通知消息人列表
        List<String> noticeUserList = noticeInfoService.getUserList(menuId);

        //存在通知列表
        if(noticeUserList != null && noticeUserList.size() > 0){
            //消息入库
            noticeUserList.forEach(userId -> {
                //重置主键id
                noticeInfo.setId(null);
                noticeInfo.setReceiveUser(userId);
                noticeInfoService.save(noticeInfo);
            });
        }


        //提醒前台页面刷新消息提醒
        WsSupport.sendMessageToAll("refresh");
    }
}
